package com.zone.vehicle.superApps.view.home;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import com.zone.vehicle.superApps.databinding.FragmentHomeBinding;
import com.zone.vehicle.superApps.model.HomeViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.base.BaseFragment;
import com.zone.vehicle.superApps.view.common.listener.ActionEnum;
import com.zone.vehicle.superApps.view.common.listener.ActionListener;
import com.zone.vehicle.superApps.view.home.app.HomeAppFragment;
import com.zone.vehicle.superApps.view.home.app.detail.AppDetailFragment;
import com.zone.vehicle.superApps.view.home.recommend.RecommendFragment;

import static com.zone.vehicle.superApps.view.home.app.HomeAppFragment.APP_HORIZONTAL_FRAGMENT;

public class HomeFragment extends BaseFragment {

    private static final String TAG = HomeFragment.class.getSimpleName();

//    public static final String HOME_APP_FRAGMENT = HomeAppFragment.class.getSimpleName();
    public static final String HOME_APP_FRAGMENT = "HomeAppFragment";
//    public static final String RECOMMEND_FRAGMENT = RecommendFragment.class.getSimpleName();
    public static final String RECOMMEND_FRAGMENT = "RecommendFragment";
//    public static final String APP_DETAIL_FRAGMENT = AppDetailFragment.class.getSimpleName();
    public static final String APP_DETAIL_FRAGMENT = "AppDetailFragment";

    private FragmentHomeBinding binding;
    private FragmentPagerAdapter adapter;
    private HomeViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initFragmentPages() {
        addPage(new HomeAppFragment());
        addPage(new RecommendFragment());
        addPage(new AppDetailFragment());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater);
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(HomeViewModel.class);
        initAdapter();
        binding.viewpager.setAdapter(adapter);
        addListener(new ActionListener() {
            @Override
            public boolean onAction(ActionEnum action) {
                Log.e(TAG, "onAction="+ action);
                return false;
            }

            @Override
            public boolean onPageChanged(String pageName) {
                Log.e(TAG, "onPageChanged="+ pageName);
                switch (pageName){
                    case APP_HORIZONTAL_FRAGMENT:
                    case HOME_APP_FRAGMENT:
                        binding.viewpager.setCurrentItem(0);
                        return true;
                    case RECOMMEND_FRAGMENT:
                        binding.viewpager.setCurrentItem(1);
                        return true;
                    case APP_DETAIL_FRAGMENT:
                        binding.viewpager.setCurrentItem(2);
                        return true;
                }
                return false;
            }
        });
        return binding.home;
    }

    private void initAdapter(){
        adapter = new FragmentPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public int getCount() {
                return getPages().size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return getPage(position);
            }
        };
    }

}
